package net.zoneland.knowledge.utils;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.branchitech.utils.enums.EnumExtend;
import net.zoneland.knowledge.enums.SanHuiYiKeColumnEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.List;

/**
 * 三会一课列宽handler.
 * @author zx
 */
public class SanHuiYiKeReportColumnHandler extends AbstractColumnWidthStyleStrategy {

    /**
     * 列宽计算.
     * @param writeSheetHolder
     * @param cellDataList
     * @param cell
     * @param head
     * @param integer
     * @param isHead
     */
    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer integer, Boolean isHead) {
        final boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
        if (needSetWidth) {
            final Sheet sheet = writeSheetHolder.getSheet();
            final int index = cell.getColumnIndex();
            setColumnWidth(sheet, SanHuiYiKeColumnEnum.getByIntKey(index));
        }
    }

    /**
     * 设置列宽.
     * @param sheet sheet对象
     * @param columnEnum 列
     */
    public static void setColumnWidth(final Sheet sheet, final EnumExtend columnEnum) {
        sheet.setColumnWidth(columnEnum.getIntKey(), Integer.valueOf(columnEnum.getStringKey()));
    }
}
